package org.hs.phd.common.frameviewer;

import java.awt.Component;
import java.awt.Point;
import java.awt.Polygon;
import java.awt.image.BufferedImage;

import javax.swing.JScrollPane;

import com.googlecode.javacv.cpp.opencv_core.IplImage;

public class DefaultFrameViewerImpl implements FrameViewer, FrameViewerTransformation {


	private ViewerCanvas viewerCanvas;
	private JScrollPane jScrollPane;
	
	public DefaultFrameViewerImpl(){
		viewerCanvas = new ViewerCanvas(this);
		jScrollPane = new JScrollPane(viewerCanvas);
	}
	
	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#getUI()
	 */
	@Override
	public Component getUI() {
		return jScrollPane;
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#addLayer(org.hs.phd.common.frameviewer.ViewerLayer)
	 */
	@Override
	public void addLayer(ViewerLayer layer) {
		layer.setViewer(this);
		viewerCanvas.addMouseListener(layer);
		viewerCanvas.addMouseMotionListener(layer);
		viewerCanvas.addLayer(layer);
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#setImage(com.googlecode.javacv.cpp.opencv_core.IplImage)
	 */
	@Override
	public void setImage(IplImage image) {
		viewerCanvas.setImage(image.getBufferedImage());
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#setImage(java.awt.image.BufferedImage)
	 */
	@Override
	public void setImage(BufferedImage image) {
		viewerCanvas.setImage(image);
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#repaint()
	 */
	@Override
	public void repaint() {
		viewerCanvas.repaint();
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#zoomIn()
	 */
	@Override
	public void zoomIn() {
		viewerCanvas.setScale( viewerCanvas.getScale() * 2.0F );
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#zoomOut()
	 */
	@Override
	public void zoomOut() {
		viewerCanvas.setScale( viewerCanvas.getScale() / 2.0F );
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#viewToModel(int, int)
	 */
	@Override
	public Point viewToModel(int x, int y) {
		return viewerCanvas.viewToModel(x, y);
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#modelToView(int, int)
	 */
	@Override
	public Point modelToView(int x, int y) {
		return viewerCanvas.modelToView(x, y);
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#modelToView(java.awt.Polygon)
	 */
	@Override
	public Polygon modelToView(Polygon polygon) {
		Polygon result = new Polygon();
		
		for(int i = 0; i < polygon.npoints; i++){
			Point p = modelToView(polygon.xpoints[i], polygon.ypoints[i]);
			result.addPoint(p.x, p.y);
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#viewToModel(java.awt.Polygon)
	 */
	@Override
	public Polygon viewToModel(Polygon polygon) {
		Polygon result = new Polygon();
		
		for(int i = 0; i < polygon.npoints; i++){
			Point p = viewToModel(polygon.xpoints[i], polygon.ypoints[i]);
			result.addPoint(p.x, p.y);
		}
		return result;
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#setScale(float)
	 */
	@Override
	public void setScale(float scale) {
		viewerCanvas.setScale(scale);
	}

	/* (non-Javadoc)
	 * @see org.hs.phd.common.frameviewer.FrameViewer#getScale()
	 */
	@Override
	public float getScale(){
		return viewerCanvas.getScale();
	}

	@Override
	public FrameViewerTransformation getTransformation() {
		return this;
	}
}
